from admin.settings.mysql.mysql_settings import mysql_auth
from admin.settings.mysql.MySQL_DB import DB_Connect


class hostModel(object):


    def __init__(self):
        self.db_connect = DB_Connect(mysql_auth)


    def get_hostlist(self):
        sql = '''
            select 
                h.id, hc.`name` as category_name, h.name, h.ip_addr, h.port, h.remark, h.system_type
            from host h
            inner join host_category hc on h.category_id = hc.id;
        '''

        data = self.db_connect.run_select_sql(sql)
        return data


    def get_host_category(self):
        sql = '''
            select `id`, `name` from `host_category`;
        '''

        data = self.db_connect.run_select_sql(sql)
        return data


    def get_host_by_list(self, host_ids):
        '''
        sql语句查询实例: select * from host where id in ('1', '2');
        '''
        query_args = ','.join("'" + str(x) + "'" for x in host_ids)

        sql = '''
            select * from host where id in (%s);
        ''' % (query_args)
        print(sql)

        data = self.db_connect.run_select_sql(sql)
        return data


    def get_host_count(self):
        sql = '''
            select count(id) as host_count from host;
        '''

        data = self.db_connect.run_select_sql(sql)
        return data[0]['host_count']

